/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tutwiz.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.tutwiz.model.User;
import net.tutwiz.persistence.UserPersistenceManager;


/**
 *
 * @author danielwirdehall
 */
public class UserController extends Controller {

//    @EJB
    private UserPersistenceManager pm = new UserPersistenceManager();

    public UserController(String[] params, HttpServletRequest request, HttpServletResponse response) {
        super(params, request, response);
    }

	// Anropar metoden för den action som ska köras
    @Override
    public void doAction() {
        if ("".equals(this.action)) {
            // Default action
            this.all();
        } else if ("all".equals(this.action)) {
            this.all();
        }else if ("show".equals(this.action)) {
            this.show();
        }
    }

    private void all() {
        
        List<User> users = pm.getAll();
        this.render(users);
        
    }
    
     private void show() {
        
        
        if (parameters.length > 0) {
			int id;
			try {
				id = Integer.parseInt(parameters[0]);
			} catch (Exception ex) {
				id = -1;
			}
			if (id > 0) {
                            User user = pm.get(Long.valueOf(id));
                            this.render(user);
			} else {
				this.niceDie("User ID must be an integer!", 400);
			}
		} else {
			this.niceDie("User ID missing!", 400);
		}
    }
}
